﻿using System;
using System.Diagnostics;
using DirectCanvas;
using DirectCanvas.Misc;

namespace WindowsFormsTest.Demos.CompositorDemo
{
    class VisualElement
    {
        private readonly RectangleF m_location;
        private Color4 m_color;
        private RotationParameters m_rotation;
        private Random m_random;
        private PointF m_currentPos;
        private PointF m_originalPos;
        private RectangleF m_sourceArea;
        private RectangleF m_destArea;

        private static Stopwatch stopWatch;
        private static long m_lastTicks;

        private float m_animationStep = 2;

        static VisualElement()
        {
            stopWatch = new Stopwatch();
            stopWatch.Start();
        }

        public VisualElement(RectangleF area)
        {
            long ticks = stopWatch.ElapsedTicks;

            if (ticks <= m_lastTicks)
                ticks = m_lastTicks+1;

            m_random = new Random((int)ticks);

            m_lastTicks = ticks;


            m_originalPos = new PointF(area.Location.X, area.Location.Y);
            m_location = area;
            m_animationStep = m_random.Next(0, 40);
         

            float r = m_random.Next(90, 100) / 100f;
            float g = m_random.Next(90, 100) / 100f;
            float b = m_random.Next(90, 100) / 100f;
            
            //Color = new Color4(1, r, g, b);
            Color = new Color4(1, 1, 1, 1);

            m_currentPos = new PointF(m_originalPos.X, m_originalPos.Y);
        }

        public Color4 Color
        {
            get { return m_color; }
            set { m_color = value; }
        }

        public RotationParameters Rotation
        {
            get { return m_rotation; }
            set { m_rotation = value; }
        }

        public RectangleF SourceArea
        {
            get { return m_sourceArea; }
            set { m_sourceArea = value; }
        }

        public RectangleF DestArea
        {
            get { return m_destArea; }
            set { m_destArea = value; }
        }

        private PointF rotationCenter = new PointF(0.5f, 0.5f);

        public void Compose(DrawingLayer outputLayer, DrawingLayer inputLayer)
        {
            m_rotation.RotationCenter = rotationCenter;
            outputLayer.ComposeLayer(inputLayer, ref m_sourceArea, ref m_destArea, ref m_rotation, ref m_color);
        }

        public void Update(bool mouseDown, PointF mousePoint)
        {
            //m_rotation.RotateX += 5;
            //m_rotation.RotateZ += 5;
            m_rotation.RotateY += m_animationStep/4f;

            PointF targetLocation;

            if (mouseDown)
            {
                targetLocation = mousePoint;
            }
            else
            {
                targetLocation = m_originalPos;
            }

            var xDistance = targetLocation.X - m_currentPos.X;

            if (xDistance < 0)
            {
                m_currentPos.X -= m_animationStep;
            }
            else if (xDistance > 0)
            {
                m_currentPos.X += m_animationStep;
            }

            var yDistance = targetLocation.Y - m_currentPos.Y;

            if (yDistance < 0)
            {
                m_currentPos.Y -= m_animationStep;
            }
            else if (yDistance > 0)
            {
                m_currentPos.Y += m_animationStep;
            }

            DestArea = new RectangleF(m_currentPos.X, m_currentPos.Y, m_location.Width, m_location.Height);

            SourceArea = m_location;
        }
    }
}
